<?php
App::import('Controller', 'Lil.LilApp');
/**
 * Short description for CmsMenusController.php
 *
 */
/**
 * CmsMenusController class
 *
 * @uses          LilAppController
 * @package       LilCms
 * @subpackage    LilCms.Controller
 */
class CmsMenusController extends LilAppController {
/**
 * name property
 *
 * @var string 'LilMenus'
 */
	public $name = 'CmsMenus';
	
	public $uses = array('LilCms.CmsMenu');
	
/**
 * isAuthorized method
 *
 * @return bool
 */
	public function isAuthorized() {
		if (!parent::isAuthorized()) return false;
		if (isset($this->Auth) && !Configure::read('Lil.allowUsersAnything')) {
			return $this->currentUser->role('admin');
		}
		return true;
	}
/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index() {
		$this->paginate = array(
			'order'      => 'CmsMenu.lft',
			'contain'    => array(),
			'recursive'  => -1
		);
		
		$this->set('data', $this->CmsMenu->find('all'));
	}
/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add() {
		$this->setAction('admin_edit');
		$this->view = 'admin_edit';
	}
/**
 * admin_edit method
 *
 * @param int $id
 * @access public
 * @return void
 */
	function admin_edit($id = null) {
		if (!empty($this->data)) {
			if ($this->CmsMenu->save($this->data)) {
				$this->setFlash(__d('lil_cms', 'Menu has been saved.', true));
				$this->doRedirect(array('admin' => true, 'action' => 'index'));
			} else {
				$this->Session->setFlash(__d('lil_cms', 'Please verify that the information is correct.', true), 'error');
			}
		} else if (is_numeric($id) && !($this->data = $this->CmsMenu->read(null, $id))) {
			return $this->error404();
		}
		
		$this->setupRedirect();
		
		// generate parent list
		$p = $this->CmsMenu->generateTree('both');
		$this->set('parents', $p['simple']);
		$this->set('tree', $p['threaded']);
	}
/**
 * admin_move_up method
 *
 * @param int $id
 * @return void
 */
	public function admin_move_up($id = null) {
		$this->CmsMenu->moveUp($id);
		$this->redirect(array('action' => 'admin_index'));
	}
/**
 * admin_move_down method
 *
 * @param int $id
 * @return void
 */
	public function admin_move_down($id = null) {
		$this->CmsMenu->moveDown($id);
		$this->redirect(array('action' => 'admin_index'));
	}
/**
 * admin_delete method
 *
 * @param int $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->CmsMenu->recursive = -1;
		if (!empty($id) && $data = $this->CmsMenu->findById($id)) {
			$this->CmsMenu->delete($id);
			$this->setFlash(__d('lil_cms', 'Menu has been deleted.', true));
			$this->redirect($this->referer());
		} else {
			return $this->error404();
		}
	}
}